An equation in $y$ and $x$ is an algebraic expression involving an equality with two (or more) variables. An example might be $x^2 + y^2 = 1$.
The solutions to an equation in the variables $x$ and $y$ are all points $(x,y)$ which satisfy the equation.
The graph of an equation is just the set of solutions to the equation represented in the Cartesian plane.
With this definition, the graph of a function $f(x)$ is just the graph of the equation $y = f(x)$.
In general, graphing an equation is more complicated than graphing a function. For a function, we know for a given value of $x$ what the corresponding value of $f(x)$ is through evaluation of the function. For equations, we may have 0, 1 or more $y$ values for a given $x$ and even more problematic is we may have no rule to find these values.
The ImplicitEquations package provides an algorithm to plot equations. The easiest use is for an equation of the form $F(x,y) = c$, for some constant $c$.
To plot such an equation, we just need to describe the function $F$ and provide a viewing window:
using Plots, ImplicitEquations F(x,y) = x^2 + y^2 plot(F == 4, (-5, 5), (-5, 5))
The graphs aren't very pretty – they really plot pixels only – but you should see a circle of radius 2 within the viewing window $[-5,5] \times [-5,5]$. The equation to graph was represented by a function of two variables and the equality operator, == was used to specify what to draw.
More complicated equations are possible. The Devils curve has the form
$$~ y^4 - x^4 + ay^2 + bx^2 = 0 ~$$Here we draw the curve for a particular choice of $a$ and $b$. The default viewing window of $[-5,5] \times [-5,5]$ is used:
a,b = -1,2 F(x,y) = y^4 - x^4 + a*y^2 + b*x^2 plot(F == 0)
The graph $x^2 + y^2 = 1$ has tangent lines at all points except $(-1,0)$ and $(0, 1)$ and even there, one may call the vertical lines $x=-1$ and $x=1$ tangent lines. However, to recover the slope would need us to express $y$ as a function of $x$ and then differentiate that function. Of course, in this example, we would need two functions: $f(x) = \sqrt{1-x^2}$ and $g(x) = - \sqrt{1-x^2}$ to do this completely.
In general though, we may not be able to solve for $y$ in terms of $x$. What then?
The idea is to assume that $y$ is representable by some function of $x$. This makes sense, moving on the curve from $(x,y)$ to some nearby point, means changing $x$ will cause some change in $y$. With this assumption, asking what $dy/dx$ is has an obvious meaning – what is the slope at the tangent line at $(x,y)$. This can be investigated by differentiating both sides of the equation to derive a new equation.
For example, starting with $x^2 + y^2 = 1$, differentiating both sides in $x$ gives:
$$~ 2x + 2y\cdot \frac{dy}{dx} = 0. ~$$The chain rules was used to find $d/dx$ of $y^2$. From this we can solve for $dy/dx$:
$$~ dy/dx = -x/y ~$$This says the slope of the tangent line depends on the point $(x,y)$ through the formula $-x/y$.
As a check, we compare to what we would have found had we solved for $y= \sqrt{1 - x^2}$ (for $(x,y)$ with $y \geq 0$). We would have found: $dy/dx = 1/2 \cdot 1/\sqrt{1 - x^2} \cdot -2x$. Which can be simplified to $-x/y$. This should show that the method above – assuming $y$ is a function of $x$ and differentiating – is easier and more general.
This method is called implicit differentiation, as we assume $y$ is implicitly defined in terms of $x$, though we don't have an explicit formula. According to the Implicit Function Theorem the above method will work provided the curve has sufficient smoothness near the point $(x,y)$.
Consider the serpentine equation
$$~ x^2y + a\cdot b \cdot y - a^2 \cdot x = 0, \quad a\cdot b > 0. ~$$For $a = 2, b=1$ we have the graph:
F(x,y; a=2, b=1) = x^2*y + a * b * y - a^2 * x plot(F == 0, (-2, 2), (-2, 2))
We can see that at each point in the viewing window the tangent line exists due to the smoothness of the curve. Moreover, at a point $(x,y)$ the tangent will have slope $dy/dx$ satisfying:
$$~ 2xy + x^2 \frac{dy}{dx} + a\cdot b \frac{dy}{dx} - a^2 = 0. ~$$Solving, yields:
$$~ \frac{dy}{dx} = \frac{a^2 - 2xy}{ab + x^2}. ~$$The eight curve has representation
$$~ x^4 = a^2(x^2-y^2), \quad a \neq 0. ~$$A graph for $a=3$ shows why it has the name it does:
F(x,y; a=3) = x^4 - a^2*(x^2 - y^2) plot(F==0, (-3, 3), (-2, 2))
The tangent line at $(x,y)$ will have slope, $dy/dx$ satisfying:
$$~ 4x^3 = a^2 \cdot (2x - 2y \frac{dy}{dx}). ~$$Solving gives:
$$~ \frac{dy}{dx} = -\frac{4x3 - a^2 \cdot 2x}{a^2 \cdot 2y}. ~$$The point $(3,0)$ can be seen to be a solution to the equation and should have a vertical tangent line. This also is reflected in the formula, as the denominator is $a^2 2 y$, which is $0$ at this point, whereas the numerator is not.
The quotient rule can be hard to remember, unlike the product rule. No reason to despair, the product rule plus implicit differentiation can be used to recover the quotient rule. Suppose $y=f(x)/g(x)$, then we could also write $y g(x) = f(x)$. Differentiating implicitly gives:
$$~ \frac{dy}{dx} g(x) + y g'(x) = f'(x). ~$$Solving for $dy/dx$ gives:
$$~ \frac{dy}{dx} = \frac{f'(x) - y g'(x)}{g(x)}. ~$$Not quite what we expect, perhaps, but substituting in $f(x)/g(x)$ for $y$ gives us the usual formula:
$$~ \frac{dy}{dx} = \frac{f'(x) - \frac{f(x)}{g(x)} g'(x)}{g(x)} = \frac{f'(x) g(x) - f(x) g'(x)}{g(x)^2}. ~$$Let's see how to add a graph of a tangent line to the graph of an equation. Tangent lines are tangent at a point, so we need a point to discuss.
Returning to the equation for a circle, $x^2 + y^2 = 1$, let's look at $(\sqrt{2}/2, - \sqrt{2}/2)$. The derivative is $ -y/x$, so the slope at this point is $1$. The line itself has equation $y = b + m \cdot (x-a)$. The following represents this in Julia"
F(x,y) = x^2 + y^2 a,b = sqrt(2)/2, -sqrt(2)/2 m = -a/b tl(x) = b + m * (x-a)
tl (generic function with 1 method)
To graph this, we now define a function of two variables to represent the function tl. Basically, it is an algebraic equivalent of y=tl(x):
G(x,y) = y - tl(x)
G (generic function with 1 method)
Now we want to plot both $F==1$ and $G==0$. The graph will be either the one of $F$ or the one of $G$. As with other operations, | means "or" (though precedence rules require the parentheses):
plot((F == 1) | (G == 0), (-3, 3), (-3, 3))
We can see that combining terms with the logical operator | will request that both equations be rendered.
Alternatively, we can add a line with plot!, as follows:
plot(F == 1, (-3, 3), (-3, 3)) plot!(x -> b + m*(x-a), -3, 3)
SymPy can be used to do implicit differentiation. Let's do so for the Trident of Newton. This can be represented in Cartesian form as follows:
We represent it as follows:
c,d,e,h = 1,1,1,1 F(x,y) = x*y G(x,y) = c*x^3 + d*x^2 + e*x + h
G (generic function with 1 method)
Now we approach this with SymPy.
We define variable x and y and a symbolic function $u$:
using SymPy
x, y = symbols("x, y", real=true)
u = SymFunction("u") # otherwise: symbols("u", cls = SymFunction)
(anonymous function)
Defining a symbolic function is done through SymFunction. (This name is slightly modified from using SymPy under Python.) The object u is symbolic function, and u(x) a symbolic expression involving a function. This is what we use to refer to y.
To define the equation $xy=cx^3 + dx^2 + ex + h$ we can either move everything to one side, or use SymPy's Eq function, as follows:
eqn = Eq(F(x,y), G(x,y))
As we assume $y$ is a function of $x$, called u(x), this substitution is just a renaming:
eqn1 = subs(eqn, y, u(x))
At this point, we differentiate both sides in x:
eqn2 = diff(eqn1, x)
The next step is solve for $dy/dx$ – the lone answer to the linear equation – which is done as follows:
dydx = diff(u(x), x) ex = solve(eqn2, dydx)[1]
As this represents an answer in terms of u(x), we replace that term with the original variable:
dydx = subs(ex, u(x), y)
If x and y are the variable names, this function will combine the steps above:
function dy_dx(eqn)
u = SymFunction("u")
eqn1 = subs(eqn, y, u(x))
eqn2 = solve(diff(eqn1, x), diff(u(x), x))[1]
subs(eqn2, u(x), y)
end
dy_dx (generic function with 1 method)
If we have a point on the curve, such as $(1,4)$, then we can draw a tangent line with these commands:
H(x,y) = F(x,y) - G(x,y) a,b = 1, 4 m = N(subs(dydx, (x,a), (y,b))) TL(x,y) = y - (b + m * (x-a)) plot((H == 0) | (TL == 0))
SymPy has the plot_implicit function for plotting implicit equations, but this works with PyPlot, not Gadfly, so we use the ImplicitEquations package.
When we assume $y$ is a function of $x$, it may not be feasible to actually find the function algebraically. However, in many cases one can found numerically. Suppose $F(x,y) = c$ describes the equation. Then for a fixed $x$, $y(x)$ solves $F(x,y(x))) - c = 0$, so $y(x)$ is a zero of a known function. As long as we can piece together which $y$ goes with which, we can find the function.
For example, folium of Descartes has the equation
$$~ x^3 + y^3 = 3axy. ~$$Setting $a=1$, it has the graph:
a = 1 F(x,y) = x^3 + y^3 - 3*a*x*y plot(F==0)
We can solve for the lower curve, $y$, as a function of $x$, as follows:
using Roots y1(x) = minimum(fzeros(y->F(x,y), -10, 10))
y1 (generic function with 1 method)
This gives the lower part of the curve, which we can plot with:
plot(y1, -5, 5)
Though, in this case, the cubic equation would admit a closed-form solution, the approach illustrated applies more generally.
Implicit differentiation can be used to find $d^2y/dx^2$ or higher order derivatives. At each stage, the same technique is applied. The only "trick" is that some simplifications can be made.
For example, consider $x^3 - y^3=3$. To find $d^2y/dx^2$, we first find $dy/dx$:
$$~ 3x^2 - (3y^2 \frac{dy}{dx}) = 0. ~$$We could solve for $dy/dx$ at this point – it always appears as a linear factor – to get:
$$~ \frac{dy}{dx} = \frac{3x^2}{3y^2} = \frac{x^2}{y^2}. ~$$However, we differentiate the first equation, as we generally try to avoid the quotient rule
$$~ 6x - (6y \frac{dy}{dx} \cdot \frac{dy}{dx} + 3y^2 \frac{d^2y}{dx^2}) = 0. ~$$Again, if must be that $d^2y/dx$ appears as a linear factor, so we can solve for it:
$$~ \frac{d^2y}{dx^2} = \frac{6x - 6y (\frac{dy}{dx})^2}{3y^2}. ~$$One last substitution for $dy/dx$ gives:
$$~ \frac{d^2y}{dx^2} = \frac{-6x + 6y (\frac{x^2}{y^2})^2}{3y^2} = -2\frac{x}{y^2} + 2\frac{x^4}{y^5} = 2\frac{x}{y^2}(1 - \frac{x^3}{y^3}) = 2\frac{x}{y^5}(y^3 - x^3) = 2 \frac{x}{y^5}(-3). ~$$It isn't so pretty, but that's all it takes.
To visualize, we plot implicitly and notice that:
F(x,y) = x^3 - y^3 plot(F == 3, (-3, 3), (-3, 3))
The same problem can be done symbolically. The steps are similar, though the last step (replacing $x^3 - y^3$) isn't done without explicitly asking.
x, y = symbols("x, y")
u = symbols("u", cls=SymFunction)
eqn = F(x,y) - 3
eqn1 = subs(eqn, y, u(x))
dydx = solve(diff(eqn1,x), diff(u(x), x))[1] # 1 soln
d2ydx2 = solve(diff(eqn1, x, 2), diff(u(x),x, 2))[1] # 1 soln
eqn2 = subs(d2ydx2, (diff(u(x), x), dydx), (u(x), y))
simplify(eqn2)
An inverse function for $f(x)$ is a function $g(x)$ satisfying: $y = f(x)$ if and only if $g(y) = x$ for all $x$ in the domain of $f$ and $y$ in the range of $f$.
This means that when defined, $f(g(x)) = x$ and $g(f(x)) = x$.
Not all functions will have an inverse function, only those functions which are one-to-one. Strictly increasing or decreasing functions will have inverse functions. We call these functions monotonic.
The typical example is $f(x) = x^2$. We "know" the inverse would be $g(x) = \sqrt{x}$. But this isn't quite right – the property won't hold for both $x < 0$ and $x > 0$ simultaneously. The trick is to redefine the function by restricting the domain so that the function is monotonic on the domain. For the function $f(x) = x^2$, we can choose to restrict $x$ to $x \geq 0$. Then in fact $g(x) = \sqrt{x}$ is an inverse function.
It is conventional to employ the notation $f^{-1}(x)$ for the inverse function.
It is also common for people to confuse this notation with that of exponents, especially when used with trigonometric functions.
The chain rule can be used to give the derivative of an inverse function when applied to $f(f^{-1}(x)) = x$. Solving gives, $[f^{-1}(x)]' = 1 / f'(g(x))$.
This is great – if we can remember the rules. If not, sometimes implicit differentiation can also help.
Consider the inverse function for the tangent, which exists when the domain of the tangent function is restricted to $(-\pi/2, \pi/2)$. The function solves $y = \tan^{-1}(x)$ or $\tan(y) = x$. Differentiating this yields:
$$~ \sec(y)^2 \frac{dy}{dx} = 1. ~$$But $\sec(y)^2 = 1 + x^2$, as can be seen by right-triangle trigonometry. This yields the formula $[\tan^{-1}(x)]' = 1(1 + x^2)$.
For a more complicated example, suppose we have a moving trajectory $(x(t), y(t))$. The angle it makes with the origin satisfies
$$~ \tan(\theta(t)) = \frac{y(t)}{x(t)}. ~$$Suppose $\theta(t)$ can be defined in terms of the inverse to some function, We can differentiate implicitly to find $\theta'(t)$ in terms of derivatives of $y$ and $x$:
$$~ \sec^2(\theta(t)) \cdot \theta'(t) = \frac{y'(t) x(t) - y(t) x'(t)}{x(t))^2}. ~$$Moving to the other side the secant term and replacing with $1 + x(t)^2$ gives an explicit, albeit complicated, expression for the derivative of $\theta$ in terms of the functions $x$ and $y$:
$$~ \theta'(t) = (1 + x(t))^2 \cdot \frac{y'(t) x(t) - y(t) x'(t)}{x(t))^2}. ~$$Many problems are best done with implicit derivatives. A video showing such a problem along with how to do it analytically is here.
This video starts with a simple question:
If you have a rope and heavy ring, where will the ring position itself due to gravity?
Well, suppose you hold the rope in two places, which we can take to be $(0,0)$ and $(a,b)$. Then let $(x,y)$ be all the possible positions of the ring that hold the rope taught. Then we have this picture:
Ring on string figure.
Since the length of the rope does not change, we must have for any admissible $(x,y)$ that:
$$~ L = \sqrt{x^2 + y^2} + \sqrt{(a-x)^2 + (b-y)^2}, ~$$where these are the two hypotenuses in the figure, as computed through Pythagorean's theorem.
If we assume that the ring will minimize the value of y subject to this constraint, can we solve for y?
We create a function to represent the equation:
F(x, y, a, b) = sqrt(x^2 + y^2) + sqrt((a-x)^2 + (b-y)^2)
F (generic function with 2 methods)
To illustrate, we need specific values of $a$, $b$, and $L$:
a,b,L = 3, 3, 10 # L > sqrt{a^2 + b^2}
F(x, y) = F(x, y, a, b)
F (generic function with 2 methods)
Our values $(x,y)$ must satisfy $f(x,y) = L$. Let's graph:
plot(F == L, (-5, 7), (-5, 7))
The graph is an ellipse, though slightly tilted.
Okay, now to find the lowest point. This will be when the derivative is $0$. We solve by assuming $y$ is a function of $x$ called u:
using SymPy
a,b,L = symbols("a,b,L", real=true)
x,y = symbols("x,y", real=true)
u = symbols("u", cls=SymFunction)
(anonymous function)
F(x,y,a,b) = sqrt(x^2 + y^2) + sqrt((a-x)^2 + (b-y)^2) eqn = F(x,y,a,b) - L
dydx = subs(eqn, y, u(x)) |> ex -> diff(ex, x) |> ex -> solve(ex, diff(u(x),x))[1] |> subs(u(x), y)
We are looking for when the tangent line has 0 slope, or when dydx is 0:
cps = solve(dydx, x)
There are two answers, as we could guess from the graph, but we want the one for the smallest value of $y$, which is the second.
The values of dydx depend on any pair (x,y), but our solution must also satisfy the equation. That is for our value of x, we need to find the corresponding y. This should be possible by substituting:
eqn1 = subs(eqn, (x, cps[2]))
Then trying to solve with solve(eqn1, y). This doesn't work though, as SymPy can't complete the problem.
However, we can solve numerically if we fix numeric values for a, b, abd L:
f(w) = N(subs(eqn1, (a,3), (b,3), (L,10), (y,w)))
f (generic function with 1 method)
Then f will return a numeric value. We can use this function with the fzero function of the Roots package:
using Roots ystar = fzero(f, -3)
-3.2696960070847285
Okay, now we need to put this value back into our expression for the x value and also substitute in for the parameters:
xstar = N(subs(cps[2], (y,ystar), (a,3), (b,3), (L,3)))
1.0282718234751367
Our minimum is at (xstar, ystar), as this graphic shows:
F(x,y) = F(x,y, 3, 3) tl(x,y) = y - (ystar + 0 * (x- xstar)) plot((F == 10) | (tl == 0), (-10, 10), (-10, 10))
If you watch the video linked to above, you will see that the surprising fact here is the resting point is such that the angles formed by the rope are the same. Basically this makes the tension in both parts of the rope equal, so there is a static position (if not static, the ring would move and not end in the final position). We can verify this fact numerically by showing the arctangents of the two triangles are the same up to a sign (and slight round-off error):
a0, b0 = 0,0 # the foci of the ellipse are (0,0) and (3,3) a1, b1 = 3, 3 atan((b0 - ystar)/(a0 - xstar)) + atan((b1 - ystar)/(a1 - xstar)) # 0
-2.220446049250313e-16
Now, were we lucky and just happened to take $a=3$, $b = 3$ in such a way to make this work? Well, no. But convince yourself by doing the above for different values of $b$.
Is $(1,1)$ on the graph of
$$~ x^2 - 2xy + y^2 = 1? ~$$for the equation
$$~ x^2y + 2y - 4 x = 0, ~$$If $x=4$, what is a value for $y$ such that $(x,y)$ is a point on the graph of the equation?
For the equation
$$~ (y-5)\cdot \cos(4\cdot \sqrt{(x-4)^2 + y^2)} = x\cdot\sin(2\sqrt{x^2 + y^2}) ~$$is the point $(5,0)$ a solution?
Let $(x/3)^2 + (y/2)^2 = 1$. Find the slope of the tangent line at the point $(3/\sqrt{2}, 2/\sqrt{2})$.
The lame curves satisfy:
$$~ (\frac{x}{a})^n + (\frac{y}{b})^n = 1. ~$$An ellipse is when $n=1$. Take $n=3$, $a=1$, and $b=2$.
Find a positive value of $y$ when $x=1/2$.
What expression gives $dy/dx$?
Let $y - x^2 = -\log(x)$. At the point $(1/2, 0.9431...)$, the graph has a tangent line. Find this line, then find its intersection point with the $y$ axes.
This intersection is:
The witch of Agnesi is the curve given by the equation
$$~ y(x^2 + a^2) = a^3. ~$$If $a=1$, numerically find a a value of $y$ when $x=2$.
What expression yields $dy/dx$ for this curve:
For the equation of an ellipse:
$$~ (\frac{x}{a})^2 + (\frac{y}{b})^2 = 1, ~$$compute $d^2y/dx^2$. Is this the answer?
$$~ \frac{d^2y}{dx^2} = -\frac{b^2}{a^2\cdot y} - \frac{b^4\cdot x^2}{a^4\cdot y^3} = -\frac{1}{y}\frac{b^2}{a^2}(1 + \frac{b^2 x^2}{a^2 y^2}). ~$$If $y>0$ is the sign positive or negative?
If $x>0$ is the sign positive or negative?
When $x>0$, the graph of the equation is...